package com.atguigu.springcloud.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

/**
 * 限流规则配置类
 */
@Configuration
public class KeyResolverConfiguration {

    //@Bean
    public KeyResolver pathKeyResolver(){
        //根据路径限流
        return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
    }

    //@Bean
    public KeyResolver parameterKeyResolver(){
        //根据参数限流
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
    }

    @Bean
    public KeyResolver ipKeyResolver(){
        //根据参数限流
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
}
